package com.example.bysj_sb.dao;


import com.example.bysj_sb.domain.ProfTitle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("profTitleDao")
public class TitleDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //增加
    public boolean add(ProfTitle profTitle) {
        String sql = "INSERT INTO profTitle (no,description,remarks) VALUES (?,?,?)";
        int affectedRowNum = jdbcTemplate.update(sql, profTitle.getNo(), profTitle.getDescription(),profTitle.getRemarks());
        if (affectedRowNum != 1) {
            return false;
        } else {

            return true;
        }
    }

    public ProfTitle  find(int id){
        String sql = "SELECT * FROM ProfTitle WHERE id = ?";
        List<ProfTitle> profTitles = jdbcTemplate.query(sql, new Object[]{id},
                new BeanPropertyRowMapper<>(ProfTitle.class));
        if(!profTitles.isEmpty()){
            return profTitles.get(0);
        } else
            return null;
    }
    public List<ProfTitle> findAll(){
        String sql = "SELECT * FROM ProfTitle";

        List<ProfTitle > profTitles = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ProfTitle .class){

        });
        return profTitles;
    }
    public boolean update(ProfTitle profTitle){
        String sql = "UPDATE ProfTitle  SET no = ? ,description=? WHERE id = ?";
        int affectedRowNum = jdbcTemplate.update(sql,profTitle.getNo(),profTitle.getDescription(),profTitle.getId());
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
    public boolean delete(int id){
        String sql = "DELETE FROM ProfTitle  WHERE id = ? ";
        int affectedRowNum = jdbcTemplate.update(sql,id);
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
}

